<?php
namespace App\DBs\Home;

use Illuminate\Support\Facades\DB;
use App\DBs\BaseDB as Base;

class FrontDB extends Base
{
    //前台菜单

    public function __construct()
    {
    }

    //客户端统计
    public function setClient($server)
    {
//        array(52) {
//            ["USER"]=>
//            string(6) "apache"
//            ["HOME"]=>
//            string(16) "/usr/share/httpd"
//            ["HTTP_COOKIE"]=>
//            string(56) "laravel_session=sCZNAKLBXnXTVKFYfx9eargYRB2zqeFnEO7EABig"
//            ["HTTP_ACCEPT_LANGUAGE"]=>
//            string(14) "zh-CN,zh;q=0.9"
//            ["HTTP_ACCEPT_ENCODING"]=>
//            string(13) "gzip, deflate"
//            ["HTTP_REFERER"]=>
//            string(41) "http://www.bioplus.org.cn/home/index.html"
//            ["HTTP_ORIGIN"]=>
//            string(25) "http://www.bioplus.org.cn"
//            ["HTTP_X_REQUESTED_WITH"]=>
//            string(14) "XMLHttpRequest"
//            ["HTTP_USER_AGENT"]=>
//            string(116) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
//            ["HTTP_ACCEPT"]=>
//            string(3) "*/*"
//            ["HTTP_CACHE_CONTROL"]=>
//            string(8) "no-cache"
//            ["HTTP_PRAGMA"]=>
//            string(8) "no-cache"
//            ["HTTP_CONTENT_LENGTH"]=>
//            string(1) "0"
//            ["HTTP_CONNECTION"]=>
//            string(10) "keep-alive"
//            ["HTTP_HOST"]=>
//            string(18) "www.bioplus.org.cn"
//            ["REDIRECT_STATUS"]=>
//            string(3) "200"
//            ["SERVER_NAME"]=>
//            string(18) "www.bioplus.org.cn"
//            ["SERVER_PORT"]=>
//            string(2) "80"
//            ["SERVER_ADDR"]=>
//            string(14) "172.30.221.252"
//            ["REMOTE_PORT"]=>
//            string(5) "50109"
//            ["REMOTE_ADDR"]=>
//            string(14) "122.234.137.42"
//            ["SERVER_SOFTWARE"]=>
//            string(12) "nginx/1.20.2"
//            ["GATEWAY_INTERFACE"]=>
//            string(7) "CGI/1.1"
//            ["REQUEST_SCHEME"]=>
//            string(4) "http"
//            ["SERVER_PROTOCOL"]=>
//            string(8) "HTTP/1.1"
//            ["DOCUMENT_ROOT"]=>
//            string(16) "/www/meet/public"
//            ["DOCUMENT_URI"]=>
//            string(10) "/index.php"
//            ["REQUEST_URI"]=>
//            string(10) "/api/home/"
//            ["SCRIPT_NAME"]=>
//            string(10) "/index.php"
//            ["CONTENT_LENGTH"]=>
//            string(1) "0"
//            ["CONTENT_TYPE"]=>
//            string(0) ""
//            ["REQUEST_METHOD"]=>
//            string(4) "POST"
//            ["QUERY_STRING"]=>
//            string(13) "s=//api/home/"
//            ["PATH_TRANSLATED"]=>
//            string(16) "/www/meet/public"
//            ["PATH_INFO"]=>
//            string(0) ""
//            ["SCRIPT_FILENAME"]=>
//            string(26) "/www/meet/public/index.php"
//            ["FCGI_ROLE"]=>
//            string(9) "RESPONDER"
//            ["PHP_SELF"]=>
//            string(10) "/index.php"
//            ["REQUEST_TIME_FLOAT"]=>
//            float(1651885149.2983)
//            ["REQUEST_TIME"]=>
//            int(1651885149)
//            ["APP_ENV"]=>
//            string(3) "dev"
//            ["APP_DEBUG"]=>
//            string(4) "true"
//            ["APP_KEY"]=>
//            string(27) "qwertyujiopasdfghjklzxcvbnm"
//            ["APP_TIMEZONE"]=>
//            string(3) "PRC"
//            ["DB_CONNECTION"]=>
//            string(5) "mysql"
//            ["DB_HOST"]=>
//            string(9) "127.0.0.1"
//            ["DB_PORT"]=>
//            string(4) "3306"
//            ["DB_DATABASE"]=>
//            string(4) "meet"
//            ["DB_USERNAME"]=>
//            string(4) "root"
//            ["DB_PASSWORD"]=>
//            string(14) "*SHIyanshi2022"
//            ["DOMAIN"]=>
//            string(26) "http://121.43.117.193:8080"
//            ["LUMEN_LOG_PATH"]=>
//            string(22) "/www/meet/public/logs/"
//        }
        $upData = [];
        if (isset($server['COMPUTERNAME'])&&$server['COMPUTERNAME']){
            $upData[] = [
                'cn'=>'计算机名',
                'en'=>'COMPUTERNAME',
                'v'=>$server['COMPUTERNAME'],
            ];
        }
        if (isset($server['NUMBER_OF_PROCESSORS'])&&$server['NUMBER_OF_PROCESSORS']){
            $upData[] = [
                'cn'=>'处理器的数量',
                'en'=>'NUMBER_OF_PROCESSORS',
                'v'=>$server['NUMBER_OF_PROCESSORS'],
            ];
        }
        if (isset($server['PROCESSOR_ARCHITECTURE'])&&$server['PROCESSOR_ARCHITECTURE']){
            $upData[] = [
                'cn'=>'处理器架构',
                'en'=>'PROCESSOR_ARCHITECTURE',
                'v'=>$server['PROCESSOR_ARCHITECTURE'],
            ];
        }
        if (isset($server['PROCESSOR_ARCHITEW6432'])&&$server['PROCESSOR_ARCHITEW6432']){
            $upData[] = [
                'cn'=>'处理器架构6432',
                'en'=>'PROCESSOR_ARCHITEW6432',
                'v'=>$server['PROCESSOR_ARCHITEW6432'],
            ];
        }
        if (isset($server['PROCESSOR_IDENTIFIER'])&&$server['PROCESSOR_IDENTIFIER']){
            $upData[] = [
                'cn'=>'处理器标识符',
                'en'=>'PROCESSOR_IDENTIFIER',
                'v'=>$server['PROCESSOR_IDENTIFIER'],
            ];
        }
        if (isset($server['PROCESSOR_LEVEL'])&&$server['PROCESSOR_LEVEL']){
            $upData[] = [
                'cn'=>'处理器级别',
                'en'=>'PROCESSOR_LEVEL',
                'v'=>$server['PROCESSOR_LEVEL'],
            ];
        }
        if (isset($server['PROCESSOR_ARCHITECTURE'])&&$server['PROCESSOR_ARCHITECTURE']){
            $upData[] = [
                'cn'=>'处理器修订版',
                'en'=>'PROCESSOR_ARCHITECTURE',
                'v'=>$server['PROCESSOR_ARCHITECTURE'],
            ];
        }
        if (isset($server['HTTP_USER_AGENT'])&&$server['HTTP_USER_AGENT']){
            $upData[] = [
                'cn'=>'用户协议标识',
                'en'=>'HTTP_USER_AGENT',
                'v'=>$server['HTTP_USER_AGENT'],
            ];
        }
        if (isset($server['HTTP_ACCEPT_LANGUAGE'])&&$server['HTTP_ACCEPT_LANGUAGE']){
            $upData[] = [
                'cn'=>'HTTP接收语言',
                'en'=>'HTTP_ACCEPT_LANGUAGE',
                'v'=>$server['HTTP_ACCEPT_LANGUAGE'],
            ];
        }
        if (isset($server['HTTP_ACCEPT_ENCODING'])&&$server['HTTP_ACCEPT_ENCODING']){
            $upData[] = [
                'cn'=>'HTTP接收编码',
                'en'=>'HTTP_ACCEPT_ENCODING',
                'v'=>$server['HTTP_ACCEPT_ENCODING'],
            ];
        }
        if (isset($server['HTTP_X_REQUESTED_WITH'])&&$server['HTTP_X_REQUESTED_WITH']){
            $upData[] = [
                'cn'=>'HTTP_X_请求了',
                'en'=>'HTTP_X_REQUESTED_WITH',
                'v'=>$server['HTTP_X_REQUESTED_WITH'],
            ];
        }
        if (isset($server['HTTP_ACCEPT'])&&$server['HTTP_ACCEPT']){
            $upData[] = [
                'cn'=>'HTTP_接受',
                'en'=>'HTTP_ACCEPT',
                'v'=>$server['HTTP_ACCEPT'],
            ];
        }
        $upData = json_encode($upData,JSON_UNESCAPED_UNICODE);
        $origin = json_encode($server,JSON_UNESCAPED_UNICODE);
        $daytime = date('Y-m-d',time()).' 00:00:01';
        $res = DB::table($this->tClient)->where('client',$upData)->where('addtime','>=',$daytime)->first();
        if ($res) return array('code'=>-2,'msg'=>'已存在');
        $upData2 = ['client'=>$upData,'origin'=>$origin,'addtime'=>date('Y-m-d H:i:s',time())];
        $lastid = DB::table($this->tClient)->insertGetId($upData2);
        return array('code'=>0,'msg'=>'success','data'=>$lastid);
    }

    //Banner
    public function getBanner()
    {
        $res = DB::table($this->tFront)->where('del',0)->where('mtype',1)
            ->orderBy('id','asc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $items[] = [
                'id'=>$re['id'], 'title'=>$re['title'], 'link'=>$re['link'],
                'cn'=>$re['cn'], 'en'=>$re['en'],
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //页面语言
    public function getLang($type)
    {
        $res = DB::table($this->tLang)->where('del',0)->where('vtype',$type)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $items[] = [
                'code'=>$re['code'], 'cn'=>$re['cn'], 'en'=>$re['en'],
                'istitle'=>$re['istitle'], 'cssid'=>$re['cssid'],
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //首页横图
    public function getAds()
    {
        $items = [];
        $res = DB::table($this->tImgs)->where('del',0)->where('type',1)->get();
        if (!count($res)) return $items;
        $res = $this->jsonToArr($res);
        foreach ($res as $re) {
            $img = env('DOMAIN').'/'.$re['img'];
            $items[] = [
                'id'=>$re['id'], 'img'=>$img,
            ];
        }
        return $items;
    }

    //首页视频
    public function getVideoIndex($pages)
    {
        $db = DB::table($this->tVideo)->where('del',0);
        $total = $db->count();
        $res = $db->orderBy('id','desc')->skip($pages['start'])->take(4)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = file_exists($re['img']) ? env('DOMAIN').'/'.$re['img'] : '';
            $link = file_exists($re['link']) ? env('DOMAIN').'/'.$re['link'] : '';
            $items[] = [
                'id'=>$re['id'], 'title'=>$re['title'],
                'img'=>$img, 'link'=>$link,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items,'total'=>$total);
    }

    //首页赞助商
    public function getSponsorIndex()
    {
        $res = DB::table($this->tImgs)->where('del',0)->where('type',2)->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = file_exists($re['img']) ? env('DOMAIN').'/'.$re['img'] : '';
            $items[] = [
                'id'=>$re['id'], 'img'=>$img,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }
}